<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('base.php');

class Product extends Base {

	public function listFeaturedProducts()
	{
		$this->load->model('Product_model');
		echo json_encode(array('result'=>$this->Product_model->listFeaturedProducts()));
	}
	
	public function listProductsByTag($tagId)	{
		$this->load->model('Product_model');
		echo json_encode(array('result'=>$this->Product_model->listByTag($tagId)));
	}
	
	public function getTagDetail($tagId)	{
		$this->load->model('Tag_model');
		echo json_encode(array('result'=>$this->Tag_model->getTag($tagId)));
	}
	
	public function getPopularTags()	{
		$this->load->model('Tag_model');
		echo json_encode(array('result'=>$this->Tag_model->getPopularTags()));
	}
	
	public function search($keyword) {
		$this->load->model('Service');
		echo json_encode(array('result'=>$this->Service->search($keyword)));
	}
	
	public function checkprice($keyword) {
		$this->load->model('Service');
		echo json_encode(array('result'=>$this->Service->lookup($keyword)));
	}

	public function recommend($productId, $targetId)	{
		$this->load->library('session');
		$user = $this->session->userdata('user');
		
		$this->load->model('Recommend_model');
		$this->Recommend_model->add($user->id, $productId, $targetId);
	}
	
	public function addcart($productId)	{
		$this->load->library('session');
		$user = $this->session->userdata('user');
		$sessionId = $this->session->userdata('session_id');
		
		if ($user == null)
			$userId = 0;
		else
			$userId = $user->id;
		$this->load->model('Order_model');
		$this->Order_model->add($userId, $productId, $sessionId);
	}

	public function unlike($productId)	{
		$this->load->library('session');
		$user = $this->session->userdata('user');
		
		$this->load->model('Like_model');
		$this->Like_model->remove($user->id, $productId);
	}

	public function like($productId)	{
		$this->load->library('session');
		$user = $this->session->userdata('user');
		
		$this->load->model('Like_model');
		$this->Like_model->add($user->id, $productId);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */